package com.liner.homework.second;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args) {
        //1. Collection集合基本方法的练习：add()、size()、isEmpty()、clear()、contains()、remove()
        Collection<String> c1 = new ArrayList<>();
        c1.add("a");
        c1.add("b");
        c1.add("c");
        c1.add("d");
        c1.add("e");
        System.out.println(c1);
        System.out.println("Collection的长度是:" + c1.size());
        System.out.println("Collection的是否为空:" + c1.isEmpty());
        System.out.println("是否包含 “e” 元素:" + c1.contains("e"));
        c1.remove("d");
        System.out.println("删除 “d” 元素: " + c1);
        c1.clear();
        System.out.println("清空集合所有元素: ");
        System.out.println("集合是否为空:" + c1.isEmpty());
        //2. Collection集合间操作的练习：addAll()、containsAll()、retainAll()、removeAll()

        Collection<String> c2 = new ArrayList<>();
        c2.add("java");
        c2.add("C#");
        c2.add("C++");
        c2.add("Python");

        System.out.println("c2的集合为:" + c2);
        c1.addAll(c2);

        System.out.println("将c2值添加到c中:" + c1);
        c1.add("C");
        System.out.println("给c中添加元素后，判断是否包含c2:" + c1.containsAll(c2));
        System.out.println("取c中保留的c2的交集:" + c1.retainAll(c2));

        System.out.println("c的集合:" + c1);
        System.out.println("c2的集合:" + c2);


        System.out.println("取c中保留的c2的交集:" + c1.retainAll(c2));

        System.out.println("c的集合:" + c1);

        System.out.println("删除c中保留的c2的交集且c2不变:" + c1.removeAll(c2));

        System.out.println("c的集合:" + c1);
        System.out.println("c2的集合:" + c2);


    }
}
